package com.artezio.task.dao.impl;

import com.artezio.task.dao.*;
import com.artezio.task.model.Contact;
import com.artezio.task.model.ContactTelDetail;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: alobov
 * Date: 19.06.12
 * Time: 10:27
 * To change this template use File | Settings | File Templates.
 */

@Repository
public class ContactTelDetailDaoImpl implements ContactTelDetailDao {
    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void addNewContactTelDetail(Contact contact, ContactTelDetail contactTelDetail) {
        contactTelDetail.setContact(contact);
        contact.addContactTelDetail(contactTelDetail);
        sessionFactory.getCurrentSession().update(contact);
    }

    @Override
    public void saveContactTelDetail(ContactTelDetail contactTelDetail) {
        sessionFactory.getCurrentSession().update(contactTelDetail);
    }

    @Override
    public List<ContactTelDetail> getAllContactTelDetails() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public ContactTelDetail findContactTelDetailById(int id) {
        //String id_str = id+"";
        Criterion idEq = Restrictions.eq("id", id);
        Criteria crit = sessionFactory.getCurrentSession().createCriteria(ContactTelDetail.class);
        crit.add(idEq);
        ContactTelDetail contactTelDetail = (ContactTelDetail) crit.uniqueResult();

        return contactTelDetail;
    }

    @Override
    public void deleteContactTelDetailById(int id) {
        Session session = sessionFactory.getCurrentSession();

        Criterion idEq = Restrictions.eq("id", id);
        Criteria crit = session.createCriteria(ContactTelDetail.class);
        crit.add(idEq);
        ContactTelDetail contactTelDetail = (ContactTelDetail) crit.uniqueResult();
        session.delete(contactTelDetail);
    }

    @Override
    public void deleteContactTelDetail(ContactTelDetail contactTelDetail) {
        sessionFactory.getCurrentSession().delete(contactTelDetail);


    }

    @Override
    public List<ContactTelDetail> getContactsTelDetailByCriteria() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
